package com.giaothong.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;

import com.giaothong.R;
import com.giaothong.http.core.HttpResult;

/**
 * Show custom dialog. Custom dialog with it own style!
 */
public class DialogUtil {
	/**
	 * Show dialog with string resource
	 * 
	 * @param context : context that dialog will be shown
	 * @param titleResId : title string resource id
	 * @param messageResId : message string resource id
	 * @return
	 */
	public static Dialog showDialog(Context context, int titleResId, int messageResId) {
		return showDialog(context, titleResId, messageResId, null);
	}

	/**
	 * Show dialog with string resource
	 * 
	 * @param context : context that dialog will be shown
	 * @param titleResId : title string resource id
	 * @param messageResId : message string resource id
	 * @param listener
	 * @return
	 */
	public static Dialog showDialog(Context context, int titleResId, int messageResId, DialogOnClickListener listener) {
		String title = context.getResources().getString(titleResId);
		String message = context.getResources().getString(messageResId);

		return showDialog(context, title, message, listener);
	}

	/**
	 * Show dialog
	 * 
	 * @param context context that dialog will be shown
	 * @param title title of dialog
	 * @param message message of dialog
	 * @return
	 */
	public static Dialog showDialog(Context context, String title, String message, final DialogOnClickListener listener) {
		// check context. If not check here, sometimes it can be crashed
		if (context == null)
			return null;
		Activity activity = (Activity) context;
		if (activity.isFinishing())
			return null;

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title).setMessage(message).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				if (listener != null) {
					listener.onOKButtonOnClick();
				}
			}
		});
		AlertDialog alert = builder.create();
		alert.show();

		return alert;
	}

	/**
	 * Show dialog click ok action
	 * 
	 * @param context context that dialog will be shown
	 * @param title title of dialog
	 * @param message message of dialog
	 * @return
	 */
	public static Dialog showDialogOkClick(Context context, int titleResId, int messageResId, int titleOk, DialogInterface.OnClickListener clickListener) {
		// check context. If not check here, sometimes it can be crashed
		if (context == null)
			return null;
		Activity activity = (Activity) context;
		if (activity.isFinishing())
			return null;

		String title = context.getResources().getString(titleResId);
		String message = context.getResources().getString(messageResId);

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title).setMessage(message).setCancelable(false).setPositiveButton(titleOk, clickListener);
		AlertDialog alert = builder.create();
		alert.show();

		return alert;
	}

	/**
	 * Show confirm dialog with string resource (Yes/No dialog)
	 * 
	 * @param contextcontext that dialog will be shown
	 * @param titleResId title string resource id
	 * @param messageResId message string resource id
	 * @param listener handle event when click button Yes/No
	 * @return
	 */
	public static Dialog showConfirmDialog(Context context, int titleResId, int messageResId, final ConfirmDialogOnClickListener listener) {
		String title = context.getResources().getString(titleResId);
		String message = context.getResources().getString(messageResId);

		return showConfirmDialog(context, title, message, listener);
	}

	/**
	 * Show confirm dialog (Yes/No dialog)
	 * 
	 * @param context context that dialog will be shown
	 * @param title title of dialog
	 * @param message message of dialog
	 * @param okResId
	 * @param cancelResId
	 * @param listener handle event when click button Yes/No
	 * @return
	 */
	public static Dialog showConfirmDialog(Context context, int titleResId, int messageResId, int okResId, int cancelResId, final ConfirmDialogOnClickListener listener) {
		// check context. If not check here, sometimes it can be crashed
		if (context == null)
			return null;
		Activity activity = (Activity) context;
		if (activity.isFinishing())
			return null;

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(titleResId).setMessage(messageResId).setCancelable(false).setPositiveButton(okResId, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				if (listener != null)
					listener.onOKButtonOnClick();
			}
		}).setNegativeButton(cancelResId, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				if (listener != null)
					listener.onCancelButtonOnClick();
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();

		return alert;
	}

	/**
	 * Show confirm dialog (Yes/No dialog)
	 * 
	 * @param context context that dialog will be shown
	 * @param title title of dialog
	 * @param message message of dialog
	 * @param listener handle event when click button Yes/No
	 * @return
	 */
	public static Dialog showConfirmDialog(Context context, String title, String message, final ConfirmDialogOnClickListener listener) {
		// check context. If not check here, sometimes it can be crashed
		if (context == null)
			return null;
		Activity activity = (Activity) context;
		if (activity.isFinishing())
			return null;

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title).setMessage(message).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				if (listener != null)
					listener.onOKButtonOnClick();
			}
		}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				if (listener != null)
					listener.onCancelButtonOnClick();
				dialog.cancel();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();

		return alert;
	}

	/**
	 * Show list dialog
	 * 
	 * @param context
	 * @param titleResId
	 * @param itemsResId
	 * @param listener
	 * @return
	 */
	public static Dialog showListDialog(Context context, int titleResId, int[] itemsResId, DialogInterface.OnClickListener listener) {
		String title = context.getResources().getString(titleResId);
		String[] items = new String[itemsResId.length];
		for (int i = 0; i < itemsResId.length; i++) {
			items[i] = context.getResources().getString(itemsResId[i]);
		}

		return showListDialog(context, title, items, listener);
	}

	/**
	 * Show list dialog
	 * 
	 * @param context
	 * @param title
	 * @param items
	 * @param listener
	 * @return
	 */
	public static Dialog showListDialog(Context context, String title, String[] items, DialogInterface.OnClickListener listener) {
		// check context. If not check here, sometimes it can be crashed
		if (context == null)
			return null;
		Activity activity = (Activity) context;
		if (activity.isFinishing())
			return null;

		AlertDialog alert = null;
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title).setCancelable(true).setItems(items, listener);
		alert = builder.create();
		alert.show();

		return alert;
	}



	/**
	 * Show error dialog if request to server failed. Using in func
	 * onTaskComplete(...)
	 * 
	 * @param context
	 * @param resultCode
	 */
	public static void showErrorDialogIfRequestFailed(Context context, HttpResult resultCode) {
		if (resultCode == HttpResult.CONNECTION_ERROR || resultCode == HttpResult.TIMEOUT_ERROR || resultCode == HttpResult.UNKNOWN_ERROR) {
			// display error message dialog
			// オクレンジャーサーバとの通信に失敗したため操作が完了しませんでした。通信状態を確認して再度操作してください。
			DialogUtil.showDialog(context, R.string.setting_error, R.string.not_conection);
		}
	}

	/**
	 * using in confirm dialog
	 */
	public static interface ConfirmDialogOnClickListener {
		public void onOKButtonOnClick();

		public void onCancelButtonOnClick();
	}

	/**
	 * using in normal dialog
	 */
	public static interface DialogOnClickListener {
		public void onOKButtonOnClick();
	}
}
